package com.depot.org.user.dao;

/**
 * 
 */

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.KeyGenerator;
import com.base.LoginUser;
import com.depot.DepotConstants;
import com.depot.domains.Users;
import com.depot.org.user.params.UserParams;
import com.soar.BaseActionSupport;
import com.soar.context.Environment;
import com.soar.encrypt.SoarMd5;
import com.soar.jdbc.dbhelper.DbParameter;
import com.soar.jdbc.dbhelper.DbTemplates;
import com.soar.jdbc.dbhelper.SameTypes;
import com.soar.jdbc.dbhelper.SortTypes;
import com.soar.jdbc.dbhelper.SqlTypes;
import com.soar.jdbc.dbhelper.WhereTypes;
import com.soar.util.StringUtils;

public class UsersDao
{
	/**
	 * @param env
	 * @param domain
	 * @param action
	 * @return
	 * @throws SQLException
	 */
	public static List<HashMap<String, String>> select(Environment env, Users domain, BaseActionSupport action) throws SQLException
	{
		String rolecode = env.getLoginUser(LoginUser.class).getUserRoleCode();
		DbParameter param = new DbParameter("users T1");
		StringBuilder sb = new StringBuilder();
		sb.append("T1.ID,");
		sb.append("T1.MOBILE,");
		sb.append("T1.PASSWORD,");
		sb.append("T1.NAME,");
		sb.append("T1.EMAIL,");
		sb.append("T1.ORG_ID,");
		sb.append("T1.STATUS,");
		sb.append("(select T2.ORG_NAME from ORGANIZATION T2 where t2.ID=T1.ORG_ID) as ORG_NAME,");
		sb.append("T1.CREATE_USER,");
		sb.append("T1.CREATE_USER_ID,");
		sb.append("T1.CREATE_DATE,");
		sb.append("T1.UPDATE_USER,");
		sb.append("T1.UPDATE_USER_ID,");
		sb.append("T1.UPDATE_DATE");
		param.setSelectFields(sb.toString());
		if (StringUtils.isNoEmpty(domain.getSearchMobile()))
		{
			param.setWhereParams("MOBILE", domain.getSearchMobile(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchName()))
		{
			param.setWhereParams("NAME", domain.getSearchName(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchOrgId()))
		{
			param.setWhereParams("ORG_ID", domain.getSearchOrgId(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
		}
		if (StringUtils.isNoEmpty(domain.getSearchStatus()))
		{
			param.setWhereParams("STATUS", domain.getSearchStatus(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
		}

		if (!DepotConstants.ADMIN_CODE.equals(rolecode))
		{
			param.setWhereParams("T1.ORG_ID", env.getLoginUser().getUserOrgId(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
			param.setWhereParams("T1.STATUS", "1", SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);

		}
		if (action.getSys_p_sort_field().equals(""))
		{
			action.setSys_p_sort_field("T1.CREATE_DATE");
			action.setSys_p_sort_method(SortTypes.DESC);
		}
		param.setWhereParams("T1.ID", "000000000000000000", SqlTypes.CHAR, WhereTypes.AND, SameTypes.NO);

		param.setOrderParams(action.getSys_p_sort_field(), action.getSys_p_sort_method());
		return DbTemplates.select(env.getConnection(), param, action);

	}

	/**
	 * @param env
	 * @param domain
	 * @return
	 * @throws SQLException
	 */
	public static HashMap<String, String> doShow(Environment env, Users domain) throws SQLException
	{
		DbParameter param = new DbParameter("users T1");
		StringBuilder sb = new StringBuilder();
		sb.append("T1.ID,");
		sb.append("T1.MOBILE,");
		sb.append("T1.PASSWORD,");
		sb.append("T1.NAME,");
		sb.append("T1.EMAIL,");
		sb.append("T1.ORG_ID,");
		sb.append("(select T2.ORG_NAME from ORGANIZATION T2 where t2.ID=T1.ORG_ID) as ORG_NAME,");
		sb.append("T1.STATUS");
		param.setSelectFields(sb.toString());

		if (StringUtils.isNoEmpty(domain.getId()))
		{
			param.setWhereParams("T1.ID", domain.getId(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
		}
		else
		{
			param.setWhereParams("T1.ID", domain.getUserId(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
		}

		return DbTemplates.select(env.getConnection(), param).get(0);
	}

	public static List<HashMap<String, String>> isHasThisUser(Environment env, Users domain) throws SQLException
	{
		DbParameter param = null;
		param = new DbParameter("USERS T1");
		param.setSelectFields("T1.ID,T1.STATUS");
		param.setWhereParams("T1.MOBILE", domain.getMobile(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.YES);
		param.setWhereParams("T1.STATUS", "3", SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.NO);
		param.setWhereParams("T1.ID", domain.getId(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.NO);
		List<HashMap<String, String>> list = DbTemplates.select(env.getConnection(), param);
		return list;
	}

	/**
	 * @param env
	 * @param domain
	 * @return
	 * @throws SQLException
	 */
	public static String doSave(Environment env, Users domain) throws SQLException
	{

		List<HashMap<String, String>> ul = isHasThisUser(env, domain);
		if (ul.size() > 0)
		{
			return "2";
		}

		DbParameter param = null;
		if (StringUtils.isEmpty(domain.getId()))
		{// ID为空 新增
			param = new DbParameter("users T1");
			param.setParams("ID", KeyGenerator.getInstance().getKeyId(env.getConnection()), SqlTypes.CHAR);
			param.setParams("MOBILE", domain.getMobile(), SqlTypes.VARCHAR);
			if (StringUtils.isEmpty(domain.getPassword()))
			{
				param.setParams("PASSWORD", new SoarMd5().getMd5(UserParams.PASS_WORD), SqlTypes.CHAR);
			}
			else
			{
				param.setParams("PASSWORD", new SoarMd5().getMd5(domain.getPassword()), SqlTypes.CHAR);
			}
			param.setParams("NAME", domain.getName(), SqlTypes.VARCHAR);
			param.setParams("EMAIL", domain.getEmail(), SqlTypes.VARCHAR);
			param.setParams("ORG_ID",domain.getOrgId(), SqlTypes.CHAR);
			if (StringUtils.isNoEmpty(domain.getStatus()))
			{
				param.setParams("STATUS", domain.getStatus(), SqlTypes.CHAR);
			}
			else
			{
				param.setParams("STATUS", "1", SqlTypes.CHAR);
			}
			param.setParams("CREATE_USER", env.getLoginUser().getUserName(), SqlTypes.VARCHAR);
			param.setParams("CREATE_USER_ID", env.getLoginUser().getUserId(), SqlTypes.CHAR);
			param.setParams("CREATE_DATE", "sysdate", SqlTypes.CUSTOM);
			param.setParams("UPDATE_USER", env.getLoginUser().getUserName(), SqlTypes.VARCHAR);
			param.setParams("UPDATE_USER_ID", env.getLoginUser().getUserId(), SqlTypes.CHAR);
			param.setParams("UPDATE_DATE", "sysdate", SqlTypes.CUSTOM);
			if (DbTemplates.insert(env.getConnection(), param))
			{
				return "1";
			}
			else
			{
				return "0";
			}
		}
		else
		{// ID不为空 修改
			param = new DbParameter("users T1");
			param.setParams("MOBILE", domain.getMobile(), SqlTypes.VARCHAR);
			param.setParams("NAME", domain.getName(), SqlTypes.VARCHAR);
			param.setParams("EMAIL", domain.getEmail(), SqlTypes.VARCHAR);
			param.setParams("STATUS", domain.getStatus(), SqlTypes.CHAR);
			param.setParams("ORG_ID",domain.getOrgId(), SqlTypes.CHAR);
			param.setParams("UPDATE_USER", env.getLoginUser().getUserName(), SqlTypes.VARCHAR);
			param.setParams("UPDATE_USER_ID", env.getLoginUser().getUserId(), SqlTypes.CHAR);
			param.setParams("UPDATE_DATE", "sysdate", SqlTypes.CUSTOM);
			param.setWhereParams("ID", domain.getId(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
			if (DbTemplates.update(env.getConnection(), param))
			{
				return "1";
			}
			else
			{
				return "0";
			}
		}
	}

	public static List<HashMap<String, String>> getAllOrg(Environment env) throws SQLException
	{
		DbParameter param = new DbParameter("ORGANIZATION T1");
		StringBuilder sb = new StringBuilder();
		sb.append("T1.ID,");
		sb.append("T1.ORG_NAME,");
		param.setSelectFields(sb.toString());
		return DbTemplates.select(env.getConnection(), param);

	}

	public static Map<String, String> getrolemap(Environment env) throws SQLException
	{

		Map<String, String> rolemap = new HashMap<String, String>();

		DbParameter param = new DbParameter("USERS T1,USER_ROLE T2,APP_ROLE T3");

		param.setSelectFields("T1.ID,T3.ROLE_NAME");
		//param.setWhereParams("T1.ORG_ID", env.getLoginUser().getUserOrgId(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
		param.setWhereParams("T1.ID", "T2.USER_ID", SqlTypes.CUSTOM, WhereTypes.AND, SameTypes.YES);
		param.setWhereParams("T3. ID", "T2.ROLE_ID", SqlTypes.CUSTOM, WhereTypes.AND, SameTypes.YES);

		param.setOrderParams("T1.id", SortTypes.ASC);
		List<HashMap<String, String>> list = DbTemplates.select(env.getConnection(), param);

		for (HashMap<String, String> map : list)
		{
			if (!rolemap.containsKey(map.get("ID")))
			{
				rolemap.put(map.get("ID"), map.get("ROLE_NAME"));
			}
			else
			{
				rolemap.put(map.get("ID"), rolemap.get(map.get("ID")) + "," + map.get("ROLE_NAME"));
			}

		}

		return rolemap;

	}

	/**
	 * 是不管理员
	 * */
	public static boolean isOrgAdmin(Environment env, String userid, String orgid) throws SQLException
	{
		DbParameter param = new DbParameter("ORGANIZATION T1");
		StringBuilder sb = new StringBuilder();
		sb.append("T1.ID");
		param.setSelectFields(sb.toString());
		param.setWhereParams("T1.ID", orgid, SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
		param.setWhereParams("T1.STATUS", "1", SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.YES);
		List<HashMap<String, String>> oli = DbTemplates.select(env.getConnection(), param);

		if (oli.size() > 0)
		{
			return true;
		}
		return false;
	}
	
	public static List<HashMap<String, String>> getorg(Environment env) throws SQLException
	{
		DbParameter param = new DbParameter("ORGANIZATION T1");
		StringBuilder sb = new StringBuilder();
		sb.append("T1.ID,");
		sb.append("T1.ORG_NAME");

		param.setSelectFields(sb.toString());
		param.setWhereParams("T1.STATUS", "1", SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.YES);

		return DbTemplates.select(env.getConnection(), param);
	}

}